<?xml version='1.0' encoding='utf-8'?>

{% macro host_tasks(host) -%}
  {% for test in host.tests %}
    <task name="{{ test.name }}" role="{{ test.role }}">
      <fetch url="{{ VARIABLES.suites_zip_url }}#{{ test.location }}"/>
      <params>
        {% if test.waived %}
          <param name="_WAIVED" value="{{ test.waived }}"/>
        {% endif %}
        {% set sep = joiner(", ") %}
        <param name="_MAINTAINERS" value="{% for m in test.maintainers %}{{sep()}}{{m.name | e}} {{'<%s>' | format(m.email) | e}}{% endfor %}"/>
        {% for name, value in test.environment.items() %}
          <param name="{{ name }}" value="{{ value }}"/>
        {% endfor %}
      </params>
    </task>
  {% endfor %}
{% endmacro %}

{% macro host_include_template_list(host, role, template_list_field) -%}
  {% for template in host[template_list_field] %}
    {% include template %}
  {% endfor %}
{% endmacro %}

{% macro host_condition(arch, tree, condition,
                        parent_and_op=True, and_op=True) %}
  {% if condition is string %}
    {% if condition.startswith("T:") %}
      {% set ARCH = arch %}
      {% set TREE = tree %}
      {%- include condition[2:] -%}
    {% elif condition.startswith("H:") %}
      <hostname op="=" value="{{ condition[2:] }}"/>
    {% else %}
      {{ "Invalid string condition" / 0 }}
    {% endif %}
  {% elif condition is mapping %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "<and>" if and_op else "<or>" }}
    {% endif %}
    {% for k, v in condition.items() %}
      {% if k == "not" %}
        <not>{{ host_condition(arch=arch, tree=tree,
                               condition=v) }}</not>
      {% elif k == "or" %}
        {{ host_condition(arch=arch, tree=tree, condition=v,
                          parent_and_op=and_op, and_op=False) }}
      {% elif k == "and" %}
        {{ host_condition(arch=arch, tree=tree, condition=v,
                          parent_and_op=and_op, and_op=True) }}
      {% elif k == "templates" %}
        {{ host_condition(arch=arch, tree=tree, condition=v,
                          parent_and_op=and_op) }}
      {% elif k == "hostnames" %}
        {{ host_condition(arch=arch, tree=tree, condition=v,
                          parent_and_op=and_op) }}
      {% else %}
        {{ "Invalid host condition key" / 0 }}
      {% endif %}
    {% endfor %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "</and>" if and_op else "</or>" }}
    {% endif %}
  {% elif condition is sequence %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "<and>" if and_op else "<or>" }}
    {% endif %}
    {% for v in condition %}
      {{ host_condition(arch=arch, tree=tree, condition=v,
                        parent_and_op=and_op) }}
    {% endfor %}
    {% if and_op != parent_and_op and condition | length > 1 %}
      {{ "</and>" if and_op else "</or>" }}
    {% endif %}
  {% else %}
    {{ "Invalid host condition type" / 0 }}
  {% endif %}
{% endmacro %}

<job retention_tag="active+1">
  <whiteboard>{{ DESCRIPTION }}</whiteboard>
  {% for scene in SCENES %}
    {% set TREE = scene.tree %}
    {% set ARCH = scene.arch %}
    {% import "trees/" + TREE + ".j2" as tree with context %}
    {% for recipeset in scene.recipesets %}
      <recipeSet>
        {% for host in recipeset %}
          <recipe kernel_options="selinux=0">
            <distroRequires>
              <distro_arch op="=" value="{{ ARCH }}"/>
              {{- tree.distro_requires | default("") -}}
            </distroRequires>
            <hostRequires>
              <arch op="=" value="{{ ARCH }}"/>
              <not><hostname op="like" value="%gigabyte%"/></not>
              {{- tree.host_requires | default("") -}}
              {{ host_condition(arch=ARCH, tree=TREE, condition=host.condition) }}
            </hostRequires>
            <repos/>
            <partitions>
              {{- host_include_template_list(host, role, 'partitions_list') -}}
            </partitions>
            <ks_appends>
              {{- tree.ks_append | default("") -}}
            </ks_appends>
            <task name="/distribution/install" role="STANDALONE">
              <params/>
            </task>
            <task name="/distribution/command" role="None">
              <params>
                <param name="CMDS_TO_RUN" value="rm /usr/share/restraint/plugins/report_result.d/10_avc_check"/>
              </params>
            </task>
            <task name="/distribution/command" role="None">
              <params>
                <param name="CMDS_TO_RUN" value="sed -i 's/^#DefaultTasksMax.*/DefaultTasksMax=50%/' /etc/systemd/system.conf; systemctl daemon-reexec"/>
              </params>
            </task>
            <task name="/distribution/command" role="None">
              <params>
                <param name="CMDS_TO_RUN" value="rm /usr/share/restraint/plugins/report_result.d/01_dmesg_check"/>
              </params>
            </task>
            <task name="/distribution/command" role="None">
              <params>
                <param name="CMDS_TO_RUN" value="curl https://password.corp.redhat.com/RH-IT-Root-CA.crt &gt; /etc/pki/ca-trust/source/anchors/RH-IT-Root-CA.crt &amp;&amp; update-ca-trust extract"/>
              </params>
            </task>
            <task name="/test/misc/machineinfo">
              <fetch url="https://github.com/CKI-project/tests-beaker/archive/master.zip#test/misc/machineinfo"/>
            </task>
            <task name="/distribution/kpkginstall" role="STANDALONE">
              <fetch url="https://github.com/CKI-project/tests-beaker/archive/master.zip#distribution/kpkginstall"/>
              <params>
                <param name="KPKG_URL" value="{{ scene.kernel }}"/>
              </params>
            </task>
            {{- host_tasks(host) -}}
        </recipe>
      {% endfor %}
      </recipeSet>
    {% endfor %}
  {% endfor %}
</job>
